/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.aspas.beans;

import br.com.aspas.util.AbstractDAO;
import br.com.aspas.util.DatabaseManager;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

/**
 *
 * @author gimmli
 */
public class RelatorioBean extends AbstractDAO {

    private Integer codigoEstado;
    private Integer codigoInscricao;
    private Integer dataCurso;
    private Integer codigoStatus;
    private Integer codigoDataPortaria;
    private Integer tipoRelatorio;
    private Integer flag;
    private Integer empresaCodigo;

    public RelatorioBean() {
    }

    public Integer getCodigoEstado() {
        return codigoEstado;
    }

    public void setCodigoEstado(Integer codigoEstado) {
        this.codigoEstado = codigoEstado;
    }

    public Integer getCodigoInscricao() {
        return codigoInscricao;
    }

    public void setCodigoInscricao(Integer codigoInscricao) {
        this.codigoInscricao = codigoInscricao;
    }

    public Integer getDataCurso() {
        return dataCurso;
    }

    public void setDataCurso(Integer dataCurso) {
        this.dataCurso = dataCurso;
    }

    public Integer getCodigoDataPortaria() {
        return codigoDataPortaria;
    }

    public void setCodigoDataPortaria(Integer codigoDataPortaria) {
        this.codigoDataPortaria = codigoDataPortaria;
    }

    public Integer getCodigoStatus() {
        return codigoStatus;
    }

    public void setCodigoStatus(Integer codigoStatus) {
        this.codigoStatus = codigoStatus;
    }

    public Integer getEmpresaCodigo() {
        return empresaCodigo;
    }

    public void setEmpresaCodigo(Integer empresaCodigo) {
        this.empresaCodigo = empresaCodigo;
    }

    public Integer getFlag() {
        return flag;
    }

    public void setFlag(Integer flag) {
        this.flag = flag;
    }

    public Integer getTipoRelatorio() {
        return tipoRelatorio;
    }

    public void setTipoRelatorio(Integer tipoRelatorio) {
        this.tipoRelatorio = tipoRelatorio;
    }

    public String gerarRelatorio() {
        {
            ServletOutputStream ouputStream = null;
            try {
                File file = null;

                Login login = new Login();
                login = (Login) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("Login");

                Integer codEvento = login.getUsuarioEventoPerfil().getEvento().getCodigo();
                Connection conn = DatabaseManager.getSingleton().getConnection();

                Map parametros = new HashMap();
                parametros.put("evento", codEvento);
                parametros.put("REPORT_CONNECTION", conn);

                switch (tipoRelatorio) {
                    case 0:
                        parametros.put("flag", flag);
                        parametros.put("empresa", empresaCodigo);
                        file = new File("/opt/Relatorios/participanteEmpresaSintetico.jasper");
                        break;
                    case 1:
                        parametros.put("flag", flag);
                        parametros.put("empresa", empresaCodigo);
                        file = new File("/opt/Relatorios/participanteEmpresaAnalitico.jasper");
                        break;
                    case 2:
                        file = new File("/opt/Relatorios/participanteSemEmpresa.jasper");
                        break;
                    case 5:
                        parametros.put("flag", flag);
                        parametros.put("empresa", empresaCodigo);
                        file = new File("/opt/Relatorios/financeiroEmpresa.jasper");
                        break;
                    case 6:
                        file = new File("/opt/Relatorios/financeiroSemEmpresa.jasper");
                        break;
                    case 7:
                        file = new File("/opt/Relatorios/CaixaPagamentoDiarioMaster.jasper");
                        break;
                    case 8:
                        file = new File("/opt/Relatorios/portariaDia.jasper");
                        break;
                    case 9:
                        file = new File("/opt/Relatorios/cursosEvento.jasper");
                        break;
                    case 10:
                        file = new File("/opt/Relatorios/concessaoCortesia.jasper");
                        break;
                    case 11:
                        file = new File("/opt/Relatorios/financeiroGeral.jasper");
                        break;
                }
                byte[] bytes = null;

                bytes = JasperRunManager.runReportToPdf(file.getPath(), parametros);


                ExternalContext econtext = FacesContext.getCurrentInstance().getExternalContext();
                HttpServletResponse response = (HttpServletResponse) econtext.getResponse();
                conn.close();
                if (bytes != null && bytes.length > 0) {
                    response.setContentType("application/pdf");
                    response.setContentLength(bytes.length);
                    response.setHeader("Content-Disposition", "attachment; filename=relatorio.pdf");
                    ouputStream = response.getOutputStream();
                    ouputStream.write(bytes, 0, bytes.length);
                    ouputStream.flush();
                    ouputStream.close();
                    FacesContext.getCurrentInstance().responseComplete();
                }

            } catch (JRException ex) {
                Logger.getLogger(RelatorioBean.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(RelatorioBean.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(RelatorioBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }

    public String pesquisaEmpresaRelatorio() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("PessoaPJ");
        return "pesquisaPJRelatorio";
    }
}
